<?php
namespace app\redbag\controller;
use app\AdminRest;
use think\App;
use app\redbag\model\Record as Model;
use app\redbag\model\staffRecord;
use app\redbag\model\AdminActive;
//use app\redbag\model\Log;
use think\facade\Cache;
use think\facade\Db;


class AdminRecord extends AdminRest
{

    protected $model;

    protected $staff_record_model;
//
    protected $active_model;

    public function __construct(App $app) {
        parent::__construct($app);

        $this->model              = new Model();

        $this->staff_record_model = new staffRecord();

       $this->active_model = new AdminActive();
    }

    /**
     * @author chenniang
     * @DataTime: 2020-04-09 18:04
     * @功能说明:员工发放记录
     */
    public function staffRecordList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];
        //活动id
        $dis[] = ['a.active_id','=',$input['active_id']];

        $map   = [];
        //搜索名字
        if(!empty($input['name'])){
            //微信昵称
            $map[] = ['b.nickName','like','%'.$input['name'].'%'];
            //用户昵称
            $map[] = ['c.name','like','%'.$input['name'].'%'];
        }
        //记录
        $data = $this->staff_record_model->AdminList($dis,$map,$input['limit']);
        //活动名称
        $data['active_name'] = $this->active_model->where(['id'=>$input['active_id']])->value('title');

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-09 18:14
     * @功能说明:停止发放
     */
    public function stopRecord(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];
        //查询记录
        $data = $this->staff_record_model->recordInfo($dis);

        if(empty($data)){

            $this->errorMsg('活动未找到');
        }
        //查看是否已经停止
        if($data['status']!=0){

            $this->errorMsg('活动已结束');

        }
        Db::startTrans();
        //结束活动
        $res = $this->staff_record_model->stopGive($dis);

        Db::commit();

        return $this->success($res);
    }

    /**
     * @author chenniang
     * @DataTime: 2020-04-11 14:44
     * @功能说明:详情
     */
    public function recordInfoList(){


        $input = $this->_input;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.staff_id','=',$input['staff_id']];

        $dis[] = ['a.active_id','=',$input['active_id']];

        if(isset($input['status'])&&$input['status']!=999){

            if($input['status']==-1){

                $dis[] = ['a.status','in',[-1,0]];

            }else{
                $dis[] = ['a.status','=',$input['status']];
            }
        }

        $data = $this->model->getUserList($dis);

        $data['staff_name'] = Db::name('longbing_card_user_info')->where(['fans_id'=>$input['staff_id']])->value('name');

        if(empty($data['staff_name'])){

            $data['staff_name'] = Db::name('longbing_card_user')->where(['id'=>$input['staff_id']])->value('nickName');
        }

        return $this->success($data);
    }







}
